package org.dromara.customer.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.customer.domain.bo.TaStoreShoppingCartBo;
import org.dromara.customer.domain.vo.TaStoreShoppingCartVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.customer.domain.vo.TaShoppingCartVo;
import org.dromara.customer.domain.bo.TaShoppingCartBo;
import org.dromara.customer.service.ITaShoppingCartService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 购物车
 * 前端访问路由地址为:/customer/shoppingCart
 *
 * @author xiyin321
 * @since 2024-07-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/shoppingCart")
public class TaShoppingCartController extends BaseController {

    private final ITaShoppingCartService taShoppingCartService;

    /**
     * 查询购物车列表
     */
    @SaCheckPermission("customer:shoppingCart:list")
    @GetMapping("/list")
    public TableDataInfo<TaStoreShoppingCartVo> list(TaShoppingCartBo bo, PageQuery pageQuery) {
        return taShoppingCartService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出购物车列表
     */
    @SaCheckPermission("customer:shoppingCart:export")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TaShoppingCartBo bo, HttpServletResponse response) {
        List<TaShoppingCartVo> list = taShoppingCartService.queryList(bo);
        ExcelUtil.exportExcel(list, "购物车", TaShoppingCartVo.class, response);
    }

    /**
     * 获取购物车详细信息
     *
     * @param shoppingCartId 主键
     */
    @SaCheckPermission("customer:shoppingCart:query")
    @GetMapping("/{shoppingCartId}")
    public R<TaShoppingCartVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long shoppingCartId) {
        return R.ok(taShoppingCartService.queryById(shoppingCartId));
    }

    /**
     * 新增购物车
     */
    @SaCheckPermission("customer:shoppingCart:add")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<List<TaStoreShoppingCartBo>> add(@Validated(AddGroup.class) @RequestBody List<TaStoreShoppingCartBo> bo) {
        return R.ok(taShoppingCartService.insertByBo(bo));
    }

    /**
     * 修改购物车
     */
    @SaCheckPermission("customer:shoppingCart:edit")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TaShoppingCartBo bo) {
        return toAjax(taShoppingCartService.updateByBo(bo));
    }

    /**
     * 删除购物车
     *
     * @param shoppingCartIds 主键串
     */
    @SaCheckPermission("customer:shoppingCart:remove")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{shoppingCartIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] shoppingCartIds) {
        return toAjax(taShoppingCartService.deleteWithValidByIds(List.of(shoppingCartIds), true));
    }

}
